package com.hiwan.system.web.dict;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * DictBean相关操作类
 * @author WQ
 *
 */
public class DictBean {

	private Map<String, Object> data;

	private String dictName;

	/**
	 * 设置字典表的名字
	 * @param dictName
	 */
	public DictBean(String dictName) {
		this.dictName = dictName;
	}

	/**
	 * 得到字典表名字
	 * @return
	 */
	public String getDictName() {
		return this.dictName;
	}

	/**
	 * 设置字典表数据
	 * @param key
	 * @param value
	 */
	public void setDictValue(String key, String value) {
		if (this.data == null) {
			this.data = new LinkedHashMap<String, Object>();
		}
		this.data.put(key, value);
	}

	/**
	 * 获取字典表整体的Map数据
	 * @return
	 */
	public Map<String, Object> getDictData() {
		return this.data;
	}

	/**
	 * 得到所有option的html字符串
	 * @return
	 */
	public String getOptionsHtml(String key) {
		StringBuffer options = new StringBuffer();
		options.append("<option value=''></option>");
		Set<String> keys = this.data.keySet();
		for (Iterator<String> it = keys.iterator(); it.hasNext();) {
			String keyName = (String) it.next();
			String keyValue = (String) this.data.get(keyName);
			if (key != null && !"".equals(key) && keyName.equals(key)) {
				options.append("<option value=\"" + keyName + "\" selected>" + keyValue + "</option>");
			} else {
				options.append("<option value=\"" + keyName + "\">" + keyValue + "</option>");
			}

		}

		return options.toString();
	}

	/**
	 * 获取字典表HTML并且去除部分dictId
	 * @param selectName
	 * @param key
	 * @param noKey
	 * @return
	 */
	public String getOptionsHtml(String key, String noKey) {
		StringBuffer options = new StringBuffer();
		Set<String> keys = this.data.keySet();
		for (Iterator<String> it = keys.iterator(); it.hasNext();) {
			String keyName = (String) it.next();
			String keyValue = (String) this.data.get(keyName);
			if (noKey != null && noKey.indexOf(keyName) != -1) {
				continue;
			}
			if (key != null && !"".equals(key) && keyName.equals(key)) {
				options.append("<option value=\"" + keyName + "\" selected>" + keyValue + "</option>");
			} else {
				options.append("<option value=\"" + keyName + "\">" + keyValue + "</option>");
			}

		}

		return options.toString();
	}

	/**
	 * 得到key对应的值
	 * @param key
	 * @return
	 */
	public String getOptionValue(String key) {
		if (key != null && !"".equals(key)) {
			return nullToBlank((String) this.data.get(key));
		} else {
			return "";
		}
	}

	/**
	 * NULL到空
	 * @param str
	 * @return
	 */
	public String nullToBlank(String str) {
		if (str == null || "".equals(str)) {
			return "";
		} else {
			return str.trim();
		}
	}

}
